package com.example.vote.flow.controller;

import com.example.vote.flow.pojo.core.Principle;
import com.example.vote.flow.pojo.param.UserLoginParam;
import com.example.vote.flow.pojo.param.UserRegisterParam;
import com.example.vote.flow.pojo.vo.UserVO;
import com.example.vote.flow.service.IUserService;
import com.example.vote.flow.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.Valid;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "1. 用户管理模块")
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @ApiOperationSupport(order = 10)
    @Operation(summary = "[游客] 注册新用户")
    @PostMapping("/public/register")
    public JsonResult<Void> register(@ParameterObject @Valid UserRegisterParam param) {
        userService.register(param);
        return JsonResult.ok();
    }

    @ApiOperationSupport(order = 20)
    @Operation(summary = "[游客] 用户登录")
    @PostMapping("/public/login")
    public JsonResult<Principle> login(@ParameterObject @Valid UserLoginParam param, HttpSession session) {
        Principle principle = userService.login(param);
        session.setAttribute(Principle.SESSION_KEY, principle);
        return JsonResult.ok(principle);
    }

    @ApiOperationSupport(order = 200)
    @Operation(summary = "[管理] 删除用户")
    @Parameter(name = "id", description = "用户id", required = true, example = "1")
    @PostMapping("/manage/{id}/delete")
    public JsonResult<Void> deleteById(@PathVariable Long id) {
        userService.deleteById(id);
        return JsonResult.ok();
    }

    @ApiOperationSupport(order = 410)
    @Operation(summary = "[用户] 查询个人信息")
    @GetMapping("/current")
    public JsonResult<UserVO> selectById(HttpSession session) {
        Principle principle = (Principle) session.getAttribute(Principle.SESSION_KEY);
        UserVO data = userService.selectById(principle.getId());
        return JsonResult.ok(data);
    }

    @ApiOperationSupport(order = 410)
    @Operation(summary = "[管理] 根据ID查询用户信息")
    @Parameter(name = "id", description = "用户id", required = true, example = "1")
    @GetMapping("/manage/{id}")
    public JsonResult<UserVO> selectById(@PathVariable Long id) {
        UserVO data = userService.selectById(id);
        return JsonResult.ok(data);
    }

    @ApiOperationSupport(order = 420)
    @Operation(summary = "[管理] 查询所有用户列表")
    @GetMapping("/manage")
    public JsonResult<List<UserVO>> selectAll() {
        List<UserVO> list = userService.selectAll();
        return JsonResult.ok(list);
    }

    @ApiOperationSupport(order = 500)
    @Operation(summary = "[用户] 退出登录")
    @PostMapping("/logout")
    public JsonResult<Void> logout(HttpSession session) {
        session.invalidate();
        return JsonResult.ok();
    }

}
